package cn.gd.controller.TravelApplicationsController;

import cn.gd.pojo.Travelapplications;
import cn.gd.service.TravelApplicationsService.TravelApplicationsService;
import cn.gd.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/travel - applications")
public class TravelApplicationsController {

    @Autowired
    private TravelApplicationsService travelApplicationsService;

//    public TravelApplicationsController(TravelApplicationsService travelApplicationsService) {
//        this.travelApplicationsService = travelApplicationsService;
//    }

    // 获取所有出差申请
    @GetMapping("/findAll")
    public Result<List<Travelapplications>> findAll() {
        List<Travelapplications> list = travelApplicationsService.findAll();
        return Result.ok(list);
    }

    // 根据ID获取出差申请
    @GetMapping("/findById/{TravelID}")
    public Result<Travelapplications> findById(@PathVariable Integer TravelID) {
        Travelapplications application = travelApplicationsService.findById(TravelID);
        if (application!= null) {
            return Result.ok(application);
        }
        return Result.error();
    }

    // 新增出差申请
    @PostMapping("/save")
    public Result<Travelapplications> save(@RequestBody Travelapplications travelApplications) {
        Travelapplications saved = travelApplicationsService.save(travelApplications);
        return Result.ok(saved);
    }

    // 更新出差申请
    @PutMapping("/update/{TravelID}")
    public Result<Travelapplications> update(@PathVariable Integer TravelID, @RequestBody Travelapplications travelApplications) {
        travelApplications.setTravelID(TravelID);
        Travelapplications updated = travelApplicationsService.update(travelApplications);
        if (updated!= null) {
            return Result.ok(updated);
        }
        return Result.error();
    }

    // 删除出差申请
    @DeleteMapping("/deleteById/{TravelID}")
    public Result<Void> deleteById(@PathVariable Integer TravelID) {
        travelApplicationsService.deleteById(TravelID);
        return Result.error();
    }

    //获取审批人
    @GetMapping("/getApprover/{TravelID}")
    public Result<List<Travelapplications>> getApprover(@PathVariable Integer TravelID) {
        List<Travelapplications> approvers = travelApplicationsService.getApprover(TravelID);
        return Result.ok(approvers);
    }
}
